/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hzf.booklibrary.ui.easyadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.Constant;
import com.hzf.booklibrary.bean.Recommend;
import com.hzf.booklibrary.bean.book.Book;
import com.hzf.booklibrary.biz.ImageLoader;
import com.hzf.booklibrary.utils.LogUtils;
import com.hzf.booklibrary.view.recyclerview.EasyRecyclerView;
import com.hzf.booklibrary.view.recyclerview.adapter.BaseViewHolder;
import com.hzf.booklibrary.view.recyclerview.adapter.RecyclerArrayAdapter;

/**
 * @author yuyh.
 * @date 2016/9/7.
 */
public class RecommendAdapter extends RecyclerArrayAdapter<Book> {
    private ImageLoader imageLoader;
    private static EasyRecyclerView mRecyclerView;

    public RecommendAdapter(Context context) {
        super(context);
        imageLoader = ImageLoader.getInstance(8, ImageLoader.Type.FIFO, ImageLoader.NO_CROP, context);

    }

    public static void setRecyclerView(EasyRecyclerView RecyclerView) {
        mRecyclerView = RecyclerView;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(final ViewGroup parent, final int viewType) {
        return new BaseViewHolder<Book>(parent, R.layout.item_recommend_list) {
            @Override
            public void setData(final Book item) {
                super.setData(item);
//                holder.setText(R.id.tvRecommendTitle, item.name);
                String name = item.name;
                ImageView viewById = (ImageView) holder.getItemView().findViewById(R.id.ivRecommendCover);
                if (item.icon_path.equals("http://oss-pics-huabei5.oss-cn-huhehaote.aliyuncs.com/")) {//无封面
                    holder.setText(R.id.tvRecommendTitle,item.name);
                    if (item.filetype.equals("txt") || item.filetype.equals("TXT")) {
                        holder.setImageResource(R.id.ivRecommendCover, R.drawable.book_txt_bg);
                    } else if (item.filetype.equals("pdf") || item.filetype.equals("PDF")) {
                        holder.setImageResource(R.id.ivRecommendCover, R.drawable.book_pdf_bg);
                    } else {
                        holder.setImageResource(R.id.ivRecommendCover, R.drawable.book_txt_bg);
                    }
                } else {//有封面
//                        String tag = (String) holder.getView(R.id.ivRecommendCover).getTag();
//                        if(!path.equals(tag)){//重新适配，预防闪屏
//                            holder.setImageResource(R.id.ivRecommendCover,R.drawable.common_book_bg);
//                        }
                        imageLoader.loadImage(item.icon_path, (ImageView) holder.getView(R.id.ivRecommendCover),true);
                        TextView tvCoverName = holder.getView(R.id.tvRecommendTitle);
                        tvCoverName.setTag(item.icon_path);
                }
                imageLoader.setOnLoadImageFailedListener(new ImageLoader.OnLoadImageFailedListener() {
                    @Override
                    public void onLoadImageFailed(String tag,ImageView imageView) {
                        for (Book book:getAllData()){
                            if(book.icon_path.equals("http://oss-pics-huabei5.oss-cn-huhehaote.aliyuncs.com/")){
                                return;//无封面--防止下标越界异常
                            }
                            if(tag.equals("")){
                                imageView.setImageResource(R.drawable.book_txt_bg );
                                tag = tag.substring(Constant.API_BASE_URL.length(),tag.length());
                                LogUtils.e(tag);
                                TextView textView = (TextView) mRecyclerView.findViewWithTag(tag);
                                if(textView!=null){
                                    textView.setText(book.name);
                                }
                                break;
                            }
                        }
                    }


                });
            }
        };
    }

}
